package com.adcc.ags.gms.gw.view;

import com.adcc.ags.gms.gw.vo.TrafficVO;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * Created by xuechao on 2017/1/16.
 */
public class ExcelView extends AbstractExcelView {
    @Override
    protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<TrafficVO> result = (List<TrafficVO>) model.get("trafficMsg");
        //生成一个sheet1
        HSSFSheet sheet = workbook.createSheet("sheet1");
        //为sheet1生成第一行，用于放表头信息
        HSSFRow row = sheet.createRow(0);
        //格式设置类
        HSSFCellStyle styleTitle = workbook.createCellStyle();
        styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        HSSFCellStyle styleContent = workbook.createCellStyle();
        styleContent.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //第一行的第一个单元格的值为  ‘序号’
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("序列");
        cell.setCellStyle(styleTitle);
        cell = row.createCell(1);
        cell.setCellValue("报文类型");
        cell.setCellStyle(styleTitle);
        cell = row.createCell(2);
        cell.setCellValue("发送时间");
        cell.setCellStyle(styleTitle);
        cell = row.createCell(3);
        cell.setCellValue("发送用户");
        cell.setCellStyle(styleTitle);
        cell = row.createCell(4);
        cell.setCellValue("接收用户");
        cell.setCellStyle(styleTitle);
        cell = row.createCell(5);
        cell.setCellValue("报文原文");
        cell.setCellStyle(styleTitle);
        cell = row.createCell(6);
        cell.setCellValue("报文发送结果");
        cell.setCellStyle(styleTitle);
        for(int i=0;i<result.size();i++)
        {
            TrafficVO trafficVO = result.get(i);
            //数据每增加一行，表格就再生成一行
            row = sheet.createRow(i+1);
            //第一个单元格，放序号随着i的增加而增加
            cell = row.createCell(0);
            cell.setCellValue(i+1);
            cell.setCellStyle(styleContent);
            //第二个单元格放
            cell = row.createCell(1);
            cell.setCellValue(trafficVO.getMsgType());
            cell.setCellStyle(styleContent);
            //第三个单元格放
            cell = row.createCell(2);
            cell.setCellValue(trafficVO.getDateTime());
            cell.setCellStyle(styleContent);
            //第四个单元格放
            cell = row.createCell(3);
            cell.setCellValue(trafficVO.getSender());
            cell.setCellStyle(styleContent);
            //第五个单元格放
            cell = row.createCell(4);
            cell.setCellValue(trafficVO.getReceiver());
            cell.setCellStyle(styleContent);
            //第六个单元格放
            cell = row.createCell(5);
            cell.setCellValue(trafficVO.getRawMsg());
            cell.setCellStyle(styleContent);
            //第七个单元格放
            String msgResult = "";
            if (trafficVO.getError_code().equals("1001")) {
                msgResult = "消息验证异常";
            } else if (trafficVO.getError_code().equals("2001")) {
                msgResult = "消息过滤异常";
            } else if (trafficVO.getError_code().equals("3001")) {
                msgResult = "发送下行失败";
            } else if (trafficVO.getError_code().equals("4001")) {
                msgResult = "发送上行失败";
            } else if (trafficVO.getError_code().equals("5001")) {
                msgResult = "发送地地失败";
            } else if (trafficVO.getError_code().equals("4001,5001")) {
                msgResult = "发送上行和地地失败";
            } else {
                msgResult = "报文发送成功";
            }
            cell = row.createCell(6);
            cell.setCellValue(msgResult);
            cell.setCellStyle(styleContent);
        }
        sheet = workbook.getSheet("sheet1");
        sheet.autoSizeColumn(3);
        sheet.autoSizeColumn(4);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //这里对文件名进行编码，保证下载时汉字显示正常
        String fileName = URLEncoder.encode("报文.xls", "utf-8");
        //Content-disposition属性设置成以附件方式进行下载
        response.setHeader("Content-disposition", "attachment;filename="
                + fileName);
        OutputStream os = response.getOutputStream();
        workbook.write(os);
        os.flush();
        os.close();
    }
}
